<!--yy-stepper 计步器 -->
<template>
  <view class="yy-stepper flex flex-m">
    <view v-if="show">
      <u-icon @click="cut" :class="{ disabled: cutDisabled }" :name="cutName" color="#45C8F3" :size="20" class="icon"></u-icon>
    </view>
    <view v-if="show" class="value">{{ num }}</view>
    <view>
      <u-icon @click="add" class="icon" :size="20" color="#45C8F3" :name="addName" :class="{ disabled: addDisabled }"></u-icon>
    </view>
  </view>
</template>

<script>
  export default {
    name: 'yy-stepper',
    props: {
      max: {
        type: [Number, String]
      },
      min: {
        type: [Number, String],
        default: 0
      },
      value: {
        type: [Number, String]
      }
    },
    data() {
      return {}
    },
    computed: {
      num: {
        set(v) {
          this.$emit('input', v)
        },
        get() {
          return this.value
        }
      },
      show() {
        return +this.num >= 1
      },
      addName() {
        return !this.max || this.num >= this.max ? 'plus-circle' : 'plus-circle-fill'
      },

      cutName() {
        return this.num <= this.min ? 'minus-circle' : 'minus-circle-fill'
      },
      addDisabled() {
        return !this.max || this.num >= this.max
      },

      cutDisabled() {
        return this.num <= this.min
      }
    },
    methods: {
      add() {
        if (this.addDisabled) {
          this.$emit('clickDisabled', 'add')
          return
        }
        this.num = this.num + 1
        this.$emit('add')
        this.handelEmit('add')
      },
      cut() {
        if (this.cutDisabled) {
          this.$emit('clickDisabled', 'cut')
          return
        }
        this.num = this.num - 1
        this.$emit('cut')
        this.handelEmit('cut')
      },

      handelEmit(type) {
        this.$emit('change', type, this.num)
      }
    }
  }
</script>
<style lang="scss" scoped>
  .yy-stepper {
    .icon {
      color: $mainColor;
      font-size: 40rpx !important;
    }

    .value {
      font-size: 28rpx;
      color: #333;
      width: 40rpx;
      text-align: center;
    }
    .disabled {
      color: $anterior !important;
    }
  }
</style>
